package com.zjeav.lingjiao.ui.personCenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dalimao.corelibrary.pullrefresh.BGANormalRefreshViewHolder;
import com.dalimao.corelibrary.pullrefresh.BGARefreshLayout;
import com.dalimao.corelibrary.pullrefresh.PowerfulRecyclerView;
import com.dalimao.corelibrary.pullrefresh.TipView;
import com.dalimao.corelibrary.swipeback.SwipeBackActivity;
import com.dalimao.corelibrary.utils.NetWorkUtils;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.adapter.MyBookAdapter;
import com.zjeav.lingjiao.base.baseBean.Book;
import com.zjeav.lingjiao.base.baseBean.Province;
import com.zjeav.lingjiao.base.request.BookRequest;
import com.zjeav.lingjiao.base.request.MallBookRequest;
import com.zjeav.lingjiao.base.response.BooksResponse;
import com.zjeav.lingjiao.base.response.SubjectResponse;
import com.zjeav.lingjiao.base.utils.UIUtils;
import com.zjeav.lingjiao.ui.book.BookDetialActivity;
import com.zjeav.lingjiao.ui.book.MallBookContract;
import com.zjeav.lingjiao.ui.home.persenter.GetbookPersenter;
import com.zjeav.lingjiao.ui.home.view.GetBookContract;
import com.zjeav.lingjiao.ui.presenter.BookPresenter;

import java.util.ArrayList;

/**
 * 我的课本
 */
public class MyCourseActivity extends SwipeBackActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        BGARefreshLayout.BGARefreshLayoutDelegate, MallBookContract.View,GetBookContract.View {

    private TipView tipView;
    private BGARefreshLayout mRefreshLayout;
    private PowerfulRecyclerView mBooks;
    private MyBookAdapter myBookAdapter;
    private GetbookPersenter getbookPersenter;

    private ArrayList<Book> books = new ArrayList<>();
    private BookPresenter bookPresenter;
    private boolean ispull = true; //是否是下拉刷新
    private int index = 1;
    private int size = 10;
    private int grade = 1;
    private int subjectid = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mybooks_list_layout);

        tipView = findViewById(R.id.tip_view);
        mRefreshLayout = findViewById(R.id.refresh_layout);
        mBooks = findViewById(R.id.rv_mybooks);

        bookPresenter = new BookPresenter(this);

        myBookAdapter = new MyBookAdapter(this);
        myBookAdapter.setEnableLoadMore(true);
        myBookAdapter.setOnLoadMoreListener(this, mBooks);
        mRefreshLayout.setDelegate(this);
        mBooks.setLayoutManager(new GridLayoutManager(this, 1));
        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
        BGANormalRefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(this, false);
        // 设置下拉刷新
        refreshViewHolder.setRefreshViewBackgroundColorRes(R.color.color_F3F5F4);//背景色
        refreshViewHolder.setPullDownRefreshText(UIUtils.getString(R.string.refresh_pull_down_text));//下拉的提示文字
        refreshViewHolder.setReleaseRefreshText(UIUtils.getString(R.string.refresh_release_text));//松开的提示文字
        refreshViewHolder.setRefreshingText(UIUtils.getString(R.string.refresh_ing_text));//刷新中的提示文字
        // 设置下拉刷新和上拉加载更多的风格
        mRefreshLayout.setRefreshViewHolder(refreshViewHolder);
        mRefreshLayout.shouldHandleRecyclerViewLoadingMore(mBooks);
        mBooks.setAdapter(myBookAdapter);

        getbookPersenter = new GetbookPersenter(this);
        BookRequest bookRequest = new BookRequest(index,size);
        getbookPersenter.getbook(bookRequest);
//        MallBookRequest mallBookRequest =  new MallBookRequest(index, size);
//        bookPresenter.getFiltrateBooks(mallBookRequest);

        myBookAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.action_txt) {
                    Book book = (Book) adapter.getItem(position);
                    Intent intent = new Intent(MyCourseActivity.this, BookDetialActivity.class);
                    intent.putExtra("id", book.getId());
                    intent.putExtra("isbuy", true);
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * 上拉加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        ispull = false;
        index++;
        bookPresenter.getFiltrateBooks(new MallBookRequest(index, size, String.valueOf(subjectid), String.valueOf(grade)));
        myBookAdapter.loadMoreComplete();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        ispull =true;
        if (!NetWorkUtils.isNetworkAvailable(this)) {
            //网络不可用弹出提示
            tipView.show();
            if (mRefreshLayout.getCurrentRefreshStatus() == BGARefreshLayout.RefreshStatus.REFRESHING) {
                mRefreshLayout.endRefreshing();
            }
            return;
        }

        index = 1;
        books.clear();
        bookPresenter.getFiltrateBooks(new MallBookRequest(index, size, String.valueOf(subjectid), String.valueOf(grade)));
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }

    @Override
    public void showMallBooks(ArrayList<Book> list) {
        if (ispull) {
            tipView.show("加载数据成功");
        }

        if (list.size()<= 0) {
            myBookAdapter.loadMoreEnd();
            mRefreshLayout.endRefreshing();// 加载完毕后在 UI 线程结束下拉刷新
            return;
        }
        mRefreshLayout.endRefreshing();// 加载完毕后在 UI 线程结束下拉刷新

        books.addAll(list);
        myBookAdapter.replaceData(this.books);
    }

    @Override
    public void getGrades(ArrayList<SubjectResponse> list) {

    }

    @Override
    public void getSubjects(ArrayList<SubjectResponse> list) {

    }

    @Override
    public void showError(Throwable throwable) {
        if (ispull) {
            tipView.show("数据加载异常");
        }
        mRefreshLayout.endRefreshing();// 加载完毕后在 UI 线程结束下拉刷新
    }

    @Override
    public void showRegions(ArrayList<Province> province) {

    }


    /**
     * 获取我的课程
     * @param qniuGettokenReponse
     */
    @Override
    public void showBookSuccess(BooksResponse qniuGettokenReponse) {
        if (ispull) {
            tipView.show("加载数据成功");
        }

        if (qniuGettokenReponse.getData().size()<= 0) {
            myBookAdapter.loadMoreEnd();
            mRefreshLayout.endRefreshing();// 加载完毕后在 UI 线程结束下拉刷新
            return;
        }
        mRefreshLayout.endRefreshing();// 加载完毕后在 UI 线程结束下拉刷新

        books.addAll(qniuGettokenReponse.getData());
        myBookAdapter.replaceData(this.books);
    }

    @Override
    public void showBookFailure(Throwable t) {
        if (ispull) {
            tipView.show("数据加载异常");
        }
        mRefreshLayout.endRefreshing();// 加载完毕后在 UI 线程结束下拉刷新
    }
}
